class SlideshowsController < ApplicationController

  before_filter :set_slideshow, :only => [:edit, :preview, :update, :destroy, 
    :generate_xml, :edit_order, :cancel_reorder, :reorder, :delete_sound,:image_list]
  before_filter :set_ecase, :only => [:edit, :preview, :update, :destroy,
    :edit_order, :cancel_reorder, :reorder, :delete_sound]
  before_filter :authorize_author, :only => [:new_from_ecase, :new_from_event, :new, :create]
  before_filter :authorize_ecase_owner, :only => [:edit, :preview, :update, :destroy, 
    :edit_order, :cancel_reorder, :reorder, :delete_sound]

  # sets @slideshow variable using params[:id]
  def set_slideshow
    return false if check_params((id = params[:id]).nil?)
    return false if check_record(!Slideshow.exists?(id))
    @slideshow = Slideshow.find(id)
  end

  # sets @ecase variable using @slideshow variable
  def set_ecase
    @ecase = @slideshow.ecase
    
    if @ecase.nil?
      msg = "Slideshow #" + @slideshow.id.to_s + " neither belongs to Ecase nor Event!"
      render(:partial => "/templates/exception", :layout => "index", :object => msg) and return false
    end
  end
#  def set_event
#    return if check_params((id = params[:id]).nil?)
#    @slideshow = Event.find(params[:id])
#  end

# TODO: fix this concatenation in _video_or_slideshow_admin.rhtml "new_from_" + sth
  def new_from_ecase
    ref_id = params[:ref_id]

    return if check_params(ref_id.nil?)
    return if check_record(!Ecase.exists?(ref_id))
    @ref = Ecase.find(ref_id)
    new
  end
  
  def new_from_event
    ref_id = params[:ref_id]

    return if check_params(ref_id.nil?)
    return if check_record(!Event.exists?(ref_id))
    @ref = Event.find(ref_id)
    new
  end
   
  def new
    @slideshow = Slideshow.new
    @slideshow.save
    @ref.update_attribute("slideshow_id", @slideshow.id)
    render :partial => "slideshows/edit"
  end

  def edit
    render :partial => "/slideshows/edit" and return
  end
  
  def preview
    @ref = @slideshow.reference
    render :partial => "/slideshows/slideshow_admin" and return 
  end 
  
  def update
    return if check_params((slideshow = params[:slideshow]).nil?)

    @slideshow.update_attributes(slideshow) \
      ? set_flash_info(FlashInfo.get_notice("Slideshow was successfully updated.")) \
      : set_flash_info(FlashInfo.get_error("Could not update slideshow! Make sure sound file is valid mp3!"))

    @ref = @slideshow.reference
    redirect_to :controller => @ref.get_controller, :action => :show , :id => @ref.id, :slideshow_edition => true
  end

  def destroy
    @ref = @slideshow.reference
    @ref.slideshow = nil
    @slideshow = nil if @slideshow.destroy
    render :partial => "/ecases/video_or_slideshow_admin" and return 
  end

  def generate_xml
    render :template => "slideshows/generate_xml", :layout => false
  end

  def update_image
    slideshow_image = params[:slideshow_image]

    return if check_params(slideshow_image.nil?)

    @slideshow = Slideshow.find(slideshow_image[:slideshow_id])
    set_ecase
    return unless authorize_ecase_owner
    
    @slideshow_image = SlideshowImage.new(slideshow_image)
    @slideshow_image.save \
      ? set_flash_info(FlashInfo.get_notice("Image was successfully added.")) \
      : set_flash_info(FlashInfo.get_error("Image not added."))
    
    @ref = @slideshow.reference
    redirect_to :controller => @ref.get_controller, :action => :show, 
      :id => @ref.id, :slideshow_edition => true
  end
  
  #Displays the drag'n'drop reordering slide view.
  def edit_order
    render :partial => "sortable_image_list"
  end
  
  #Renders the default slide view.
  def cancel_reorder 
    render :partial => "editable_image_list"
  end
  
  #Reorders the slides in database and renders the default slide view..
  def reorder
    slide_list = params[:slide_list]

    return if check_params(slide_list.nil?)

    slides = @slideshow.slideshow_images(true)
    #list in params and slides in db may not be of equal sizes
    #take the smaller size
    size = slide_list.size
    if size > slides.size
      size = slides.size
    end

    for i in 0..size-1
      new_p = slide_list.index(slides[i].position.to_s).to_i   
      slides[i].update_attribute(:position, new_p)
    end
    
    render :partial => "editable_image_list"
  end
  
  #Deletes sound for given slideshow.
  def delete_sound
    @slideshow.update_attribute(:sound_url, nil)
    @ref = @slideshow.reference
    render :partial => "sound_form" and return
  end

  #Deletes given slide
  def delete_slide
    id = params[:id]

    return if check_params(id.nil?)
    return if check_record(!SlideshowImage.exists?(id))

    @slideshow_image = SlideshowImage.find(id)
    @slideshow = @slideshow_image.slideshow
    set_ecase
    return unless authorize_ecase_owner

    @slideshow_image.destroy
    render :partial => "image_list" and return    
  end
  
  #Shows slide duration form.
  def edit_slide_duration
    id = params[:id]

    return if check_params(id.nil?)
    return if check_record(!SlideshowImage.exists?(id))
    
    @slideshow_image = SlideshowImage.find(id)
    @slideshow = @slideshow_image.slideshow
    set_ecase
    return unless authorize_ecase_owner

    render :partial => "slide_duration_form"
  end

  #Updates slide duration.
  def set_slide_duration
    data = params[:slideshow_image]
    
    return if check_params(data.nil?)
    return if check_record(!SlideshowImage.exists?(id = data[:id]))

    slideshow_image = SlideshowImage.find(id)
    @slideshow = slideshow_image.slideshow
    set_ecase
    return unless authorize_ecase_owner

    slideshow_image.update_attribute(:duration, data[:duration])
    render :partial => "image_list"
  end
    
  def image_list
    render :partial => "image_list"
  end

  private
  
  # filter
  # authorizes user to be admin or author and owner of given ecase/event
  # this allows to perform several actions
  def authorize_ecase_owner
    return false unless authorize_author

    authorized = admin_authorized? || (get_session_user.id == Ecase.find(@ecase.id).author_id) 

    unless authorized
      on_not_authorized("Please login as author of this ecase.")
    end

    authorized
  end
end
